package com.zmzncs.lmtc.module.customer.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.common.util.DateTimeUtil;
import com.zmzncs.lmtc.module.customer.dao.CustInfoDao;
import com.zmzncs.lmtc.module.customer.pojo.card_record.CardRecord;
import com.zmzncs.lmtc.module.customer.dao.CardRecordDao;
import com.zmzncs.lmtc.module.customer.pojo.card_record.CardRecordDTO;
import com.zmzncs.lmtc.module.customer.pojo.card_record.ParkMontCardRecordDTO;
import com.zmzncs.lmtc.module.customer.pojo.cust_info.CustInfo;
import com.zmzncs.lmtc.module.customer.service.CardRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户-车辆服务实时状态购买记录 服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-10-12
 */
@Service
public class CardRecordServiceImpl extends ServiceImpl<CardRecordDao, CardRecord> implements CardRecordService {

    @Autowired
    CardRecordDao cardRecordDao;
    @Autowired
    CustInfoDao custInfoDao;

    @Override
    public IPage<CardRecordDTO> getCardRecordList(Page page, Long ticketId) {
        return cardRecordDao.getCardRecordList(page,ticketId);
    }

    @Override
    public IPage<CardRecordDTO> getUsingCardRecordByParkId(Page page, Long parkId) {
        IPage<CardRecordDTO> iPage = cardRecordDao.getUsingCardRecordByParkId(page, parkId);
        List<CardRecordDTO> records = iPage.getRecords();
        if (records != null && !records.isEmpty()){
            for (CardRecordDTO record : records) {
                Date beginTime = record.getBeginTime();
                Date expireTime = record.getExpireTime();
                long surplusDay = DateTimeUtil.getDistanceDays(new Date(), expireTime);
                long allDays = DateTimeUtil.getDistanceDays(beginTime, expireTime);
                record.setSurplusDay(surplusDay);
                record.setUseDay(allDays - surplusDay);
            }
        }
        return iPage;
    }

    @Override
    public IPage<ParkMontCardRecordDTO> getAllCardRecordByParkId(Integer pageNum,Integer pageSize, Long parkId) {
        if (pageNum == null){
            pageNum = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }

        Page<Object> page = new Page<>(pageNum, pageSize);
        IPage<ParkMontCardRecordDTO> iPage = cardRecordDao.getAllCardRecordByParkId(page, parkId);
        if (iPage != null && iPage.getRecords() != null && iPage.getRecords().size() > 0){
            for (ParkMontCardRecordDTO record : iPage.getRecords()) {
                int useDays = DateTimeUtil.betweenDays(new Date(), record.getBeginTime()) + 1;
                Date endDate = new Date();
                if (useDays <= 0){
                    useDays = 0;
                    endDate = record.getBeginTime();
                }
                int SurplusDays = DateTimeUtil.betweenDays(record.getExpireTime(),endDate);
                record.setUseDay(useDays);
                record.setSurplusDay(SurplusDays);

                CustInfo custInfo = custInfoDao.selectById(record.getCustomerId());
                if (custInfo != null){
                    record.setNickname(custInfo.getNickname());
                    record.setMobilePhone(custInfo.getMobilePhone());
                }
            }
        }
        return iPage;
    }
    @Override
    public IPage<CardRecordDTO> getAllCardRecordByCustomerId(Page page, Long customerId) {
        return cardRecordDao.getAllCardRecordByCustomerId(page, customerId);
    }

}
